package com.smartmedical.patient.sickcricle.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.smartmedical.common.core.constant.TokenConstants;
import com.smartmedical.common.core.domain.HealthR;
import com.smartmedical.common.security.service.TokenService;
import com.smartmedical.common.security.utils.SecurityUtils;
import com.smartmedical.patient.sickcricle.domain.SickCircle;
import com.smartmedical.patient.sickcricle.domain.SickCircleComment;
import com.smartmedical.patient.sickcricle.domain.SickCircleCommentCount;
import com.smartmedical.patient.sickcricle.domain.SickCircleCount;
import com.smartmedical.patient.sickcricle.mapper.SickCollectionMapper;
import com.smartmedical.patient.sickcricle.service.SickCollectionService;
import com.smartmedical.patient.sickcricle.util.FastUtil;
import com.smartmedical.patient.sickcricle.vo.SickCircleCommentVo;
import com.smartmedical.patient.sickcricle.vo.SickCirclePublishVo;
import com.smartmedical.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Service
@DS(value = "slave")
public class SickCollectionServiceImpl implements SickCollectionService {

    @Autowired
    HttpServletRequest request;
    @Autowired
    private SickCollectionMapper sickCollectionMapper;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private FastUtil fastUtil;

    @Override
    public HealthR publishComment(Integer sickCircleId, String content) {

        SickCircleComment sickCircleComment = createSickCircleComment(sickCircleId, content);
        Integer healthR = sickCollectionMapper.publishComment(sickCircleComment);

        if (healthR > 0) {
            return HealthR.fail("评论成功");
        }
        return HealthR.fail("评论失败");
    }


    @Override
    public HealthR adoptionProposal(Integer sickCircleId, Integer commentId) {
        sickCollectionMapper.adoptionProposal(sickCircleId, commentId);
        return HealthR.ok();

    }





    @Override
    public HealthR<Integer> addUserSickCollection(SickCircleCount sickCircleCount) {
        Integer result = sickCollectionMapper.addUserSickCollection(sickCircleCount);
        return HealthR.ok(result);
    }




    @Override
    public HealthR<Integer> cancelSickCollection(SickCircleCount sickCircleCount) {
        Integer result = sickCollectionMapper.cancelSickCollection(sickCircleCount);
        return HealthR.ok(result);
    }




    @Override
    public HealthR<List<SickCirclePublishVo>> findMySickCircleList(Integer page, Integer count) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userId = loginUser.getUserid();
        List<SickCirclePublishVo> mySickCircleList = sickCollectionMapper.findMySickCircleList(userId, page, count);
        return HealthR.ok(mySickCircleList);
    }


    @Override
    public HealthR<List<SickCircleCommentVo>> findSickCircleCommentList(Integer sickCircleId, Integer page, Integer count) {
        List<SickCircleCommentVo> sickCircleCommentList = sickCollectionMapper.findSickCircleCommentList(sickCircleId, page, count);
        return HealthR.ok(sickCircleCommentList);
    }




    @Override
    public HealthR publishSickCircle(SickCircle sickCircle) {
        sickCollectionMapper.publishSickCircle(sickCircle);
        return HealthR.ok();
    }






    @Override
    public HealthR expressOpinion(Integer commentId, Integer opinion) {



        sickCollectionMapper.expressOpinion(commentId, opinion);
        //查询观点
        Integer byOpinion = sickCollectionMapper.findByOpinion(commentId);

        //创建SickCircleCommentCount对象
        SickCircleCommentCount sickCircleCommentCount = new SickCircleCommentCount();
        //判断观点
        if (byOpinion == 1) {
            //添加支持数
            sickCollectionMapper.addSupportNum(sickCircleCommentCount.getCommentId());
            System.out.println();
            sickCollectionMapper.cancleOpposeNum(sickCircleCommentCount.getCommentId());
        }
        if (byOpinion == 2) {
            //添加反对数
            sickCollectionMapper.addOpposeNum(sickCircleCommentCount.getCommentId());
        }
        return HealthR.ok();

    }

    @Override
    public HealthR uploadSickCirclePicture(Integer sickCircleId, MultipartFile picture) {
        //图片地址
        String url = "";
        try {
            //上传图片
            url = fastUtil.upload(picture);
            if(url.equals("")){
                return HealthR.ok("http://106.14.33.194:8888/"+url,"图片上传成功");

            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return HealthR.fail("图片上传失败");

    }


    private SickCircleComment createSickCircleComment(Integer sickCircleId, String content) {
        SickCircleComment sickCircleComment = new SickCircleComment();
        sickCircleComment.setSickCircleId(sickCircleId);
        sickCircleComment.setContent(content);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long userid = loginUser.getUserid();
        sickCircleComment.setUserId(userid);
        String username = loginUser.getUsername();


        String result = sickCollectionMapper.selectUserNameDoc(username);
        if (result != null) {
            sickCircleComment.setWhetherDoctor(1);
        } else {
            sickCircleComment.setWhetherDoctor(0);
        }
        return sickCircleComment;
    }



}
